public with sharing class ctrl_IDCard{
    public Attachment file             { set; get; }
    private String parentId            { set; get; }
    public Boolean hasPicture          { set; get; }
    public contact contactObj          { set; get; } 
    public string IdCardNumber         { set; get; }
    public ctrl_IDCard( ApexPages.StandardController stdController ){
        this.parentId       = stdController.getId();
        this.contactObj     = (Contact)stdController.getRecord();
        this.hasPicture     = false;
        List<Attachment> attList = new List<Attachment>([ Select ParentId, Name, Id, ContentType, BodyLength From Attachment where ParentId =: this.parentId and name like 'Contact Picture%' order by name desc limit 1 ]);
        if( attList.size() > 0 ){
            this.file               = attList.get( 0 );
            this.hasPicture         = true;
        }
    }
    public PageReference createRecord(){
        try{
            //Create a new IDCardObject
            ID_Card__c cardObj = new ID_Card__c();
            cardObj.Contact__c = this.parentId;
            cardObj.Name__c = contactObj.Name;
            cardObj.Arabic_Name__c = contactObj.Arabic_Name__c;
            cardObj.Job_Title_Arabic__c = contactObj.Job_Title_Arabic__c;
            cardObj.Occupation__c = contactObj.Occupation__c;
            cardObj.Nationality__c = contactObj.Nationality__r.Name;
            cardObj.Company_Name_Arabic__c = contactObj.Current_Employer__r.company_name_arabic__c + ' ' + contactObj.Current_Employer__r.LegalStatus__r.Short_Name_Arabic__c;
            cardObj.Company_Name__c = contactObj.Current_Employer__r.Name + ' ' + contactObj.Current_Employer__r.LegalStatus__r.Short_Name_English__c;
            cardObj.Passport_Number__c = contactObj.Passport_Number__r.Passport_Number__c;
            cardObj.Passport_Expiry_Date__c = contactObj.Passport_Number__r.Passport_Expiry_Date__c;
            cardObj.Visa_Number__c = contactObj.Visa_Reference_Number__r.Visa_Number__c;
            cardObj.Visa_Expiry_Date__c = contactObj.Visa_Reference_Number__r.Visa_Expiry_Date__c;
            cardObj.ID_Expiry_Date__c = contactObj.Visa_Reference_Number__r.Visa_Expiry_Date__c;
            
            insert cardObj;
            
            List<ID_Card__c> idCardList = new List<ID_Card__c>([select id, name from id_card__c where id = :cardObj.id]);
            if(idCardList.size() > 0)
            {
                IdCardNumber = idCardList[0].Name;
            }
        }
        catch (exception e) {
            ApexPages.addMessages(e);
        }
        return null;
    }
}